<!DOCTYPE html>
<html>
<head>
<title>oops!homework5--lookforpositions.md</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">

<style>
/* https://github.com/microsoft/vscode/blob/master/extensions/markdown-language-features/media/markdown.css */
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

body {
	font-family: var(--vscode-markdown-font-family, -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif);
	font-size: var(--vscode-markdown-font-size, 14px);
	padding: 0 26px;
	line-height: var(--vscode-markdown-line-height, 22px);
	word-wrap: break-word;
}

#code-csp-warning {
	position: fixed;
	top: 0;
	right: 0;
	color: white;
	margin: 16px;
	text-align: center;
	font-size: 12px;
	font-family: sans-serif;
	background-color:#444444;
	cursor: pointer;
	padding: 6px;
	box-shadow: 1px 1px 1px rgba(0,0,0,.25);
}

#code-csp-warning:hover {
	text-decoration: none;
	background-color:#007acc;
	box-shadow: 2px 2px 2px rgba(0,0,0,.25);
}

body.scrollBeyondLastLine {
	margin-bottom: calc(100vh - 22px);
}

body.showEditorSelection .code-line {
	position: relative;
}

body.showEditorSelection .code-active-line:before,
body.showEditorSelection .code-line:hover:before {
	content: "";
	display: block;
	position: absolute;
	top: 0;
	left: -12px;
	height: 100%;
}

body.showEditorSelection li.code-active-line:before,
body.showEditorSelection li.code-line:hover:before {
	left: -30px;
}

.vscode-light.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(0, 0, 0, 0.15);
}

.vscode-light.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(0, 0, 0, 0.40);
}

.vscode-light.showEditorSelection .code-line .code-line:hover:before {
	border-left: none;
}

.vscode-dark.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(255, 255, 255, 0.4);
}

.vscode-dark.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(255, 255, 255, 0.60);
}

.vscode-dark.showEditorSelection .code-line .code-line:hover:before {
	border-left: none;
}

.vscode-high-contrast.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(255, 160, 0, 0.7);
}

.vscode-high-contrast.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(255, 160, 0, 1);
}

.vscode-high-contrast.showEditorSelection .code-line .code-line:hover:before {
	border-left: none;
}

img {
	max-width: 100%;
	max-height: 100%;
}

a {
	text-decoration: none;
}

a:hover {
	text-decoration: underline;
}

a:focus,
input:focus,
select:focus,
textarea:focus {
	outline: 1px solid -webkit-focus-ring-color;
	outline-offset: -1px;
}

hr {
	border: 0;
	height: 2px;
	border-bottom: 2px solid;
}

h1 {
	padding-bottom: 0.3em;
	line-height: 1.2;
	border-bottom-width: 1px;
	border-bottom-style: solid;
}

h1, h2, h3 {
	font-weight: normal;
}

table {
	border-collapse: collapse;
}

table > thead > tr > th {
	text-align: left;
	border-bottom: 1px solid;
}

table > thead > tr > th,
table > thead > tr > td,
table > tbody > tr > th,
table > tbody > tr > td {
	padding: 5px 10px;
}

table > tbody > tr + tr > td {
	border-top: 1px solid;
}

blockquote {
	margin: 0 7px 0 5px;
	padding: 0 16px 0 10px;
	border-left-width: 5px;
	border-left-style: solid;
}

code {
	font-family: Menlo, Monaco, Consolas, "Droid Sans Mono", "Courier New", monospace, "Droid Sans Fallback";
	font-size: 1em;
	line-height: 1.357em;
}

body.wordWrap pre {
	white-space: pre-wrap;
}

pre:not(.hljs),
pre.hljs code > div {
	padding: 16px;
	border-radius: 3px;
	overflow: auto;
}

pre code {
	color: var(--vscode-editor-foreground);
	tab-size: 4;
}

/** Theming */

.vscode-light pre {
	background-color: rgba(220, 220, 220, 0.4);
}

.vscode-dark pre {
	background-color: rgba(10, 10, 10, 0.4);
}

.vscode-high-contrast pre {
	background-color: rgb(0, 0, 0);
}

.vscode-high-contrast h1 {
	border-color: rgb(0, 0, 0);
}

.vscode-light table > thead > tr > th {
	border-color: rgba(0, 0, 0, 0.69);
}

.vscode-dark table > thead > tr > th {
	border-color: rgba(255, 255, 255, 0.69);
}

.vscode-light h1,
.vscode-light hr,
.vscode-light table > tbody > tr + tr > td {
	border-color: rgba(0, 0, 0, 0.18);
}

.vscode-dark h1,
.vscode-dark hr,
.vscode-dark table > tbody > tr + tr > td {
	border-color: rgba(255, 255, 255, 0.18);
}

</style>

<style>
/* Tomorrow Theme */
/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */
/* Original theme - https://github.com/chriskempson/tomorrow-theme */

/* Tomorrow Comment */
.hljs-comment,
.hljs-quote {
	color: #8e908c;
}

/* Tomorrow Red */
.hljs-variable,
.hljs-template-variable,
.hljs-tag,
.hljs-name,
.hljs-selector-id,
.hljs-selector-class,
.hljs-regexp,
.hljs-deletion {
	color: #c82829;
}

/* Tomorrow Orange */
.hljs-number,
.hljs-built_in,
.hljs-builtin-name,
.hljs-literal,
.hljs-type,
.hljs-params,
.hljs-meta,
.hljs-link {
	color: #f5871f;
}

/* Tomorrow Yellow */
.hljs-attribute {
	color: #eab700;
}

/* Tomorrow Green */
.hljs-string,
.hljs-symbol,
.hljs-bullet,
.hljs-addition {
	color: #718c00;
}

/* Tomorrow Blue */
.hljs-title,
.hljs-section {
	color: #4271ae;
}

/* Tomorrow Purple */
.hljs-keyword,
.hljs-selector-tag {
	color: #8959a8;
}

.hljs {
	display: block;
	overflow-x: auto;
	color: #4d4d4c;
	padding: 0.5em;
}

.hljs-emphasis {
	font-style: italic;
}

.hljs-strong {
	font-weight: bold;
}
</style>

<style>
/*
 * Markdown PDF CSS
 */

 body {
	font-family: -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif, "Meiryo";
	padding: 0 12px;
}

pre {
	background-color: #f8f8f8;
	border: 1px solid #cccccc;
	border-radius: 3px;
	overflow-x: auto;
	white-space: pre-wrap;
	overflow-wrap: break-word;
}

pre:not(.hljs) {
	padding: 23px;
	line-height: 19px;
}

blockquote {
	background: rgba(127, 127, 127, 0.1);
	border-color: rgba(0, 122, 204, 0.5);
}

.emoji {
	height: 1.4em;
}

code {
	font-size: 14px;
	line-height: 19px;
}

/* for inline code */
:not(pre):not(.hljs) > code {
	color: #C9AE75; /* Change the old color so it seems less like an error */
	font-size: inherit;
}

/* Page Break : use <div class="page"/> to insert page break
-------------------------------------------------------- */
.page {
	page-break-after: always;
}

</style>

<script src="https://unpkg.com/mermaid/dist/mermaid.min.js"></script>
</head>
<body>
  <script>
    mermaid.initialize({
      startOnLoad: true,
      theme: document.body.classList.contains('vscode-dark') || document.body.classList.contains('vscode-high-contrast')
          ? 'dark'
          : 'default'
    });
  </script>
<h1 id="%E5%BF%83%E7%94%B5%E4%BF%A1%E5%8F%B7%E7%9A%84qrs%E6%A3%80%E6%B5%8Bc%E8%AF%AD%E8%A8%80%E7%BC%96%E7%A8%8B">心电信号的QRS检测——C语言编程</h1>
<h2 id="%E7%AC%AC%E4%BA%94%E6%AC%A1%E4%BD%9C%E4%B8%9A">（第五次作业）</h2>
<pre><code class="language-mermaid"><div class="mermaid">stateDiagram-v2
    超过阈值-->低于阈值:寻找下降沿
    低于阈值-->超过阈值:寻找上升沿
    超过阈值-->结果:输出
    低于阈值-->结果:输出
</div></code></pre>
<h2 id="%E4%B8%80%E6%BB%A4%E6%B3%A2%E5%90%8E%E6%A3%80%E6%B5%8Bqrs%E6%B3%A2">一、滤波后检测QRS波</h2>
<h3 id="1%E4%B8%BB%E7%A8%8B%E5%BA%8F">1.主程序</h3>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"ecgfilter.h"</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"ecglib.h"</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{ 
    FILE *fp = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100.dat"</span>,<span class="hljs-string">"rb"</span>);  <span class="hljs-comment">//只读模式读取原始文件</span>
    FILE *fp_po = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100-po.dat"</span>,<span class="hljs-string">"wb"</span>);


    <span class="hljs-keyword">if</span>(fp == <span class="hljs-literal">NULL</span>) <span class="hljs-comment">//若不存在文件</span>
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"the file is not exist\r\n"</span>); 
    short x;
    <span class="hljs-keyword">int</span> icount = <span class="hljs-number">0</span>;  <span class="hljs-comment">//位置变量</span>
    <span class="hljs-keyword">while</span>(!feof(fp)) <span class="hljs-comment">//当文件没读完</span>
    {
        fread(&amp;x,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp); <span class="hljs-comment">//以1字节为单位读取2字节（short型长度）的数据放入x中</span>
        short qrs = qrsdetect(x);  <span class="hljs-comment">//检测当前位置是否正在发生qrs波</span>
        icount ++;  <span class="hljs-comment">//位置的移动？</span>
        <span class="hljs-keyword">if</span> (qrs==<span class="hljs-number">1</span> || qrs ==<span class="hljs-number">0</span>)  <span class="hljs-comment">//将上升超过阈值时和下降低于阈值时的点存入文件</span>
             fwrite(&amp;icount,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">int</span>),fp_po);  <span class="hljs-comment">//把位置存到文件里？</span>
    }
    
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello world\r\n"</span>);
    fclose(fp);  <span class="hljs-comment">//关闭文件，释放内存？</span>
    fclose(fp_po);

    <span class="hljs-keyword">return</span> <span class="hljs-number">9</span>;

}
</div></code></pre>
<h3 id="2%E8%AF%86%E5%88%AB%E5%87%BD%E6%95%B0qrsdetect">2.识别函数qrsdetect</h3>
<pre class="hljs"><code><div><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">qrsdetect</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x)</span>
</span>{
    <span class="hljs-keyword">int</span> y = qrsfilter(x);  <span class="hljs-comment">//先滤波得到积分后的方波</span>
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> star = <span class="hljs-number">0</span>;  <span class="hljs-comment">//初始是0状态</span>
    <span class="hljs-keyword">int</span> thx = <span class="hljs-number">2000</span>;  <span class="hljs-comment">//设一个阈值 </span>

    <span class="hljs-keyword">switch</span> (star)
    {
    <span class="hljs-keyword">case</span> <span class="hljs-number">0</span>:  <span class="hljs-comment">//0状态的时候 寻找上升支？</span>
        <span class="hljs-keyword">if</span>(y&gt;thx)  <span class="hljs-comment">//超过阈值（上升支）</span>
        {
            star = <span class="hljs-number">1</span>;  <span class="hljs-comment">//切换为1状态</span>
            <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;  <span class="hljs-comment">//获得上升沿的阈值临界点</span>
        }
        <span class="hljs-keyword">break</span>;
    <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>:  <span class="hljs-comment">//1状态的时候 寻找下降支？</span>
        <span class="hljs-keyword">if</span>(y&lt;thx)  <span class="hljs-comment">//低于阈值（下降支）</span>
        {
            star = <span class="hljs-number">0</span>;  <span class="hljs-comment">//切换为0状态</span>
            <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;  <span class="hljs-comment">//获得下降沿的阈值临界点</span>
        }
        <span class="hljs-keyword">break</span>;
    }
    <span class="hljs-keyword">return</span> <span class="hljs-number">9</span>;  <span class="hljs-comment">//其他时刻返回9 </span>
}
</div></code></pre>
<h3 id="3%E6%BB%A4%E6%B3%A2%E5%87%BD%E6%95%B0qrsfilter">3.滤波函数qrsfilter</h3>
<pre class="hljs"><code><div>FILE *fp_lp = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100-lp.dat"</span>,<span class="hljs-string">"wb"</span>);  <span class="hljs-comment">//只写模式</span>
FILE *fp_hp = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100-hp.dat"</span>,<span class="hljs-string">"wb"</span>);
FILE *fp_de = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100-de.dat"</span>,<span class="hljs-string">"wb"</span>);
FILE *fp_in = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100-in.dat"</span>,<span class="hljs-string">"wb"</span>);
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">qrsfilter</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x)</span>
</span>{
    <span class="hljs-comment">//也还想要获得每一步的结果试试</span>
    x = lpfilter(x);
    fwrite(&amp;x,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp_lp);  <span class="hljs-comment">//将文件每两字节一个数据写入文件中</span>
    x = hpfilter(x);
    fwrite(&amp;x,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp_hp);  
    x = derivative(x);
    fwrite(&amp;x,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp_de);
    x = integration(x*x);
    fwrite(&amp;x,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp_in);
    <span class="hljs-keyword">return</span> x;
}
</div></code></pre>
<h2 id="%E4%BA%8Cqrs%E6%B3%A2%E6%A3%80%E6%B5%8B%E7%BB%93%E6%9E%9C%E7%9A%84%E6%98%BE%E7%A4%BA">二、QRS波检测结果的显示</h2>
<h3 id="1matlab%E7%A8%8B%E5%BA%8F">1.Matlab程序</h3>
<pre class="hljs"><code><div>fs = <span class="hljs-number">250</span>; <span class="hljs-comment">%采样频率为250Hz  </span>
gain = <span class="hljs-number">200</span>; <span class="hljs-comment">%增益为200  </span>

fid2 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100.dat'</span>,<span class="hljs-string">'rb'</span>);
ecgt = fread(fid2,<span class="hljs-string">'short'</span>);
fid1 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-lp.dat'</span>,<span class="hljs-string">'rb'</span>);
ecg = fread(fid1,<span class="hljs-string">'short'</span>);
fid3 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-hp.dat'</span>,<span class="hljs-string">'rb'</span>);
ecg2 = fread(fid3,<span class="hljs-string">'short'</span>);
fid4 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-de.dat'</span>,<span class="hljs-string">'rb'</span>);
ecg3 = fread(fid4,<span class="hljs-string">'short'</span>);
fid5 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-in.dat'</span>,<span class="hljs-string">'rb'</span>);
ecg4 = fread(fid5,<span class="hljs-string">'short'</span>);
fid6 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-po.dat'</span>,<span class="hljs-string">'rb'</span>);
qrs = fread(fid6,<span class="hljs-string">'int'</span>);
<span class="hljs-comment">%%</span>
<span class="hljs-comment">%画找的位置在原始波形上的显示</span>
<span class="hljs-built_in">figure</span>;
t = (<span class="hljs-number">1</span>:<span class="hljs-built_in">length</span>(ecg))/fs; <span class="hljs-comment">%横坐标转为时间</span>
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:qrs(<span class="hljs-number">50</span>)),ecgt(<span class="hljs-number">1</span>:qrs(<span class="hljs-number">50</span>)));
<span class="hljs-built_in">hold</span> on;
<span class="hljs-built_in">plot</span>(qrs(<span class="hljs-number">1</span>:<span class="hljs-number">50</span>)/fs,ecgt(qrs(<span class="hljs-number">1</span>:<span class="hljs-number">50</span>)),<span class="hljs-string">'.r'</span>);
xlabel(<span class="hljs-string">'时间（s）'</span>);
ylabel(<span class="hljs-string">'幅度'</span>);
title(<span class="hljs-string">'找到的位置显示在原波形图上'</span>);
<span class="hljs-comment">%%</span>
<span class="hljs-comment">%画找的位置在积分图上的显示</span>
<span class="hljs-built_in">figure</span>;
t = (<span class="hljs-number">1</span>:<span class="hljs-built_in">length</span>(ecg))/fs; <span class="hljs-comment">%横坐标转为时间</span>
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:qrs(<span class="hljs-number">50</span>)),ecg4(<span class="hljs-number">1</span>:qrs(<span class="hljs-number">50</span>)));
<span class="hljs-built_in">hold</span> on;
<span class="hljs-built_in">plot</span>(qrs(<span class="hljs-number">1</span>:<span class="hljs-number">50</span>)/fs,ecg4(qrs(<span class="hljs-number">1</span>:<span class="hljs-number">50</span>)),<span class="hljs-string">'.r'</span>);
xlabel(<span class="hljs-string">'时间（s）'</span>);
ylabel(<span class="hljs-string">'幅度'</span>);
title(<span class="hljs-string">'找到的位置显示在积分图上'</span>);
</div></code></pre>
<h3 id="2%E7%BB%93%E6%9E%9C">2.结果</h3>
<p><img src="1.png" alt="">
<img src="2.png" alt=""></p>

</body>
</html>
